<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
	xmlns:f="http://java.sun.com/jsf/core"
	xmlns:h="http://java.sun.com/jsf/html"
	xmlns:p="http://primefaces.org/ui"
	xmlns:ui="http://java.sun.com/jsf/facelets">

<ui:composition template="template.xhtml">
	<ui:define name="corpo">

		<h2>Cursos</h2>
<h:panelGrid>
		<p:commandButton value="Adicionar Curso"
			oncomplete="dialogCadastroCurso.show()" />
</h:panelGrid>

		<h:form id="cadCurso">

			<p:dialog widgetVar="dialogCadastroCurso" header="Dados do Usuario"
				closable="false" resizable="false" modal="true" hideEffect="slide"
				showEffect="clip" draggable="false">

				<h:panelGrid id="displayCurso" columns="3" cellpadding="4"
					style="margin:0 auto;">

					<h:outputLabel value="Nome: " />
					<p:inputText id="nome" value="#{cursoBean.curso.nome}"
						title="Insira aqui o nome." />
					<p:message for="nome"></p:message>

					<h:outputText></h:outputText>
					<p:commandButton id="btsalvar" value="Salvar"
						rendered="#{cursoBean.curso.id == null}"
						actionListener="#{cursoBean.gravarCurso}"
						update=":formulario:tabelaCurso displayCurso"
						oncomplete="handleCadastroCursoRequest(xhr, status, args)">
					</p:commandButton>

					<p:commandButton id="btAlterar" value="Alterar"
						rendered="#{cursoBean.curso.id != null}"
						actionListener="#{cursoBean.alterarCurso}"
						update=":formulario:tabelaCurso displayCurso"
						oncomplete="handleCadastroCursoRequest(xhr, status, args)">
					</p:commandButton>

					<p:commandButton value="Voltar" update="displayCurso"
						process="@this" oncomplete="dialogCadastroCurso.hide()"
						action="#{cursoBean.limparCurso()}">
						<p:resetInput target="displayCurso" />
					</p:commandButton>
				</h:panelGrid>

			</p:dialog>

			<script type="text/javascript">
				function handleCadastroCursoRequest(xhr, status, args) {
					if (args.validationFailed || !args.gravado) {
						jQuery('#dialogCadastroCurso').effect("shake", {
							times : 3
						}, 100);
					} else {
						dialogCadastroCurso.hide();
					}
				}
			</script>

		</h:form>
		<h:form id="formulario">

			<p:dataTable id="tabelaCurso" value="#{cursoBean.cursos}" var="curso"
				rowStyleClass="#{empty rowIx or rowIx mod 2 ne 0 ? 'par' : 'impar'}"
				rowIndexVar="rowIx" emptyMessage="nenhum registro encontrado">


				<f:facet name="header">Cursos</f:facet>

				<p:column headerText="Nome">
					<h:outputText value="#{curso.nome}"></h:outputText>
				</p:column>

				
				<p:column headerText="Ações">
					<div align="center">
						<p:commandLink update=":cadCurso:displayCurso" title="Editar"
							oncomplete="dialogCadastroCurso.show()">
							<f:setPropertyActionListener value="#{curso}"
								target="#{cursoBean.curso}" />
							<p:graphicImage value="resources/imagens/editar.png" width="24px"
								height="24px" />
						</p:commandLink>
						<p:commandLink update=":formRemoveCurso:panelRemoveCurso"
							title="Remover" oncomplete="popupRemoveCurso.show()">
							<f:setPropertyActionListener value="#{curso}"
								target="#{cursoBean.curso}" />
							<p:graphicImage value="resources/imagens/delete.png" width="24px"
								height="24px" />
						</p:commandLink>
					</div>
				</p:column>




			</p:dataTable>
		</h:form>

		<h:form id="formRemoveCurso">
			<p:dialog header="Têm certeza que deseja apagar?"
				widgetVar="popupRemoveCurso" resizable="false" id="popupRemoveCurso"
				showEffect="fade" hideEffect="explode" closable="false" modal="true">

				<p:panel id="panelRemoveCurso" columns="2">

					<p:commandButton value="Sim, remover"
						action="#{cursoBean.removerCurso}"
						update=":formulario:tabelaCurso"
						oncomplete="popupRemoveCurso.hide()" />

					<p:commandButton value="Não remover"
						action="#{cursoBean.limparCurso}"
						onclick="popupRemoveCurso.hide()" />
				</p:panel>
			</p:dialog>
		</h:form>

	</ui:define>
</ui:composition>
</html>